package com.itheima.d5_outputstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * **
 *  * 完成文件的复制  掌握资源关闭的新方式  try-with-resource
 *  自动释放资源
 *  */

public class CopyDemo2 {
    public static void main(String[] args)   {
        File src = new File("C:\\Program Files\\Java\\src\\copyaaa.jpg");
        File dest = new File("D:\\Java\\copy.jpg");
        copyFile(src,dest);
    }
    public static void copyFile(File src , File dest)  {
        try(FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(dest);)
        {

            //边读边写
            byte[] bytes = new byte[1024];
            int len;
            while((len = fis.read(bytes)) != -1){
                fos.write( bytes, 0, len);

            }
            System.out.println("复制完毕！");

        }catch(Exception e){
            e.printStackTrace();
        }



















        FileOutputStream fos = null;
        FileInputStream fis = null;
       try{
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);
           //边读边写
           byte[] bytes = new byte[1024];
           int len;
           while((len = fis.read(bytes)) != -1){
               fos.write( bytes, 0, len);
           }
           fos.close();
           fis.close();
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           try{
               fos.close();
               fis.close();
           }catch (Exception e){
               e.printStackTrace();
           }

       }


    }
}
